package com.copera.cn.kaka;


import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.copera.cn.kaka.util.ActionUrls;
import com.copera.cn.kaka.util.ActivityManager;
import com.copera.cn.kaka.util.HttpUtils;
import com.copera.cn.kaka.util.JsonUtil;
import com.copera.cn.kaka.vo.PointPraiseSetVo;

/**
 * 每人每天最多点赞
 * @author Administrator
 *
 */
public class PointPraiseDayActivity extends BaseActivity{
	private RelativeLayout back;
	private String totalCount;
	private RelativeLayout re,re1,re2,re3,re4,re5,re6,re7,re8,re9,re10,re11,re12;
	private ImageView img,img1,img2,img3,img4,img5,img6,img7,img8,img9,img10,img11,img12;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityManager.getInstance().push(this);
		setContentView(R.layout.point_praise_day);
		initViews();
		initEvent();
		initValue();
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {

				case ActionUrls.FAILURE:
					Toast.makeText(PointPraiseDayActivity.this, (String) msg.obj,Toast.LENGTH_SHORT).show();
					break;
				}
			}
		};
		//强制为竖屏 
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	private void initViews(){
		back = (RelativeLayout) findViewById(R.id.back);
		re1 = (RelativeLayout)findViewById(R.id.re_01);
		re2 = (RelativeLayout)findViewById(R.id.re_02);
		re3 = (RelativeLayout)findViewById(R.id.re_03);
		re4 = (RelativeLayout)findViewById(R.id.re_04);
		re5 = (RelativeLayout)findViewById(R.id.re_05);
		re6 = (RelativeLayout)findViewById(R.id.re_06);
		re7 = (RelativeLayout)findViewById(R.id.re_07);
		re8 = (RelativeLayout)findViewById(R.id.re_08);
		re9 = (RelativeLayout)findViewById(R.id.re_09);
		re10 = (RelativeLayout)findViewById(R.id.re_10);
		re11 = (RelativeLayout)findViewById(R.id.re_11);
		re12 = (RelativeLayout)findViewById(R.id.re_12);
		img1 = (ImageView)findViewById(R.id.img_01);
		img2 = (ImageView)findViewById(R.id.img_02);
		img3 = (ImageView)findViewById(R.id.img_03);
		img4 = (ImageView)findViewById(R.id.img_04);
		img5 = (ImageView)findViewById(R.id.img_05);
		img6 = (ImageView)findViewById(R.id.img_06);
		img7 = (ImageView)findViewById(R.id.img_07);
		img8 = (ImageView)findViewById(R.id.img_08);
		img9 = (ImageView)findViewById(R.id.img_09);
		img10 = (ImageView)findViewById(R.id.img_10);
		img11 = (ImageView)findViewById(R.id.img_11);
		img12 = (ImageView)findViewById(R.id.img_12);
	}
	@SuppressLint("NewApi")
	private void initValue(){
		String presenterCount = getAppCustomer().getSetVo().getDayCount();
		if (presenterCount != null) {
			if (presenterCount.indexOf("1") != -1) {
				if(presenterCount.indexOf("10") != -1){
					img10.setVisibility(View.VISIBLE);
					img = img10;
					re = re10;
				}else if(presenterCount.indexOf("11") != -1){
					img11.setVisibility(View.VISIBLE);
					img = img11;
					re = re11;
				}else if(presenterCount.indexOf("12") != -1){
					img12.setVisibility(View.VISIBLE);
					img = img12;
					re = re12;
				}else {
					img1.setVisibility(View.VISIBLE);
					img = img1;
					re = re1;
				}
			}else if(presenterCount.indexOf("2") != -1){
				img2.setVisibility(View.VISIBLE);
				img = img2;
				re = re2;
			}else if(presenterCount.indexOf("3") != -1){
				img3.setVisibility(View.VISIBLE);
				img = img3;
				re = re3;
			}else if(presenterCount.indexOf("4") != -1){
				img4.setVisibility(View.VISIBLE);
				img = img4;
				re = re4;
			}else if(presenterCount.indexOf("5") != -1){
				img5.setVisibility(View.VISIBLE);
				img = img5;
				re = re5;
			}else if(presenterCount.indexOf("6") != -1){
				img6.setVisibility(View.VISIBLE);
				img = img6;
				re = re6;
			}else if(presenterCount.indexOf("7") != -1){
				img7.setVisibility(View.VISIBLE);
				img = img7;
				re = re7;
			}else if(presenterCount.indexOf("8") != -1){
				img8.setVisibility(View.VISIBLE);
				img = img8;
				re = re8;
			}else if(presenterCount.indexOf("9") != -1){
				img9.setVisibility(View.VISIBLE);
				img = img9;
				re = re9;
			}
			if (re != null) {
				
				re.setBackgroundResource(R.color.set_bg);
			}
		}
	}
	private void initEvent(){
		back.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();

			}
		});
		re1.setOnClickListener(new  View.OnClickListener() {
			
			public void onClick(View v) {
				setValue(re1,img1,1);
			}
		});
        re2.setOnClickListener(new  View.OnClickListener() {
			
			public void onClick(View v) {
				setValue(re2,img2,2);
			}
		});
        re3.setOnClickListener(new  View.OnClickListener() {
			
			public void onClick(View v) {
				setValue(re3,img3,3);
			}
		});
        re4.setOnClickListener(new  View.OnClickListener() {
			
			public void onClick(View v) {
				setValue(re4,img4,4);
			}
		});
        re5.setOnClickListener(new  View.OnClickListener() {
			
			public void onClick(View v) {
				setValue(re5,img5,5);
			}
		});
        re6.setOnClickListener(new  View.OnClickListener() {
			
			public void onClick(View v) {
				setValue(re6,img6,6);
			}
		});
        re7.setOnClickListener(new  View.OnClickListener() {
			
			public void onClick(View v) {
				setValue(re7,img7,7);
			}
		});
        re8.setOnClickListener(new  View.OnClickListener() {
			
			public void onClick(View v) {
				setValue(re8,img8,8);
			}
		});
        re9.setOnClickListener(new  View.OnClickListener() {
			
			public void onClick(View v) {
				setValue(re9,img9,9);
			}
		});
        re10.setOnClickListener(new  View.OnClickListener() {
			
			public void onClick(View v) {
				setValue(re10,img10,10);
			}
		});
        re11.setOnClickListener(new  View.OnClickListener() {
			
			public void onClick(View v) {
				setValue(re11,img11,11);
			}
		});
        re12.setOnClickListener(new  View.OnClickListener() {
			
			public void onClick(View v) {
				setValue(re12,img12,12);
			}
		});
	}
	@SuppressLint("NewApi")
	private void setValue(RelativeLayout relativeLayout,ImageView imgView,int num){
		
		
		if (img != null) {
			img.setVisibility(View.GONE);
		}
		if (re != null) {
			
			re.setBackgroundResource(R.color.white);
		}
		
		relativeLayout.setBackgroundResource(R.color.set_bg);
		re = relativeLayout;
		img = imgView;
		img.setVisibility(View.VISIBLE);
		totalCount=num+"";
		save("dayCount", totalCount);
		day();
	}
	// 每人每天最多点赞
		private void day() {

			new AsyncTask<String, Integer, JSONObject>() {

				@Override
				protected JSONObject doInBackground(String... params) {
					JSONObject jsonObj = null;
					try {
						String jsonStr = HttpUtils.get(params[0], getAppCustomer()
								.getHttpClient());

						if (jsonStr != null) {
							jsonObj = new JSONObject(jsonStr);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					return jsonObj;
				}

				@Override
				protected void onPostExecute(JSONObject result) {

					if (result != null) {
						try {

							PointPraiseSetVo vo = JsonUtil.toPointPraiseSetVo(result);
							getAppCustomer().setSetVo(vo);
							Message msg = handler.obtainMessage(ActionUrls.FAILURE, "设置成功");
							handler.sendMessage(msg);
							finish();

						} catch (Exception e) {
							e.printStackTrace();
							if (e.getMessage() != null) {

							}
						}
					}
				}

			}.execute(ActionUrls.URL_POINT_PRAISE_DAY + "?shopId="
					+ getAppCustomer().getShop().getShopId() +"&totalCount="+totalCount);
		}
}
